<template>
  <div class="user-info">
    <el-form ref="userInfoForm" :model="userInfoForm" :rules="rules" label-width="300px" style="width:50%;margin-top:40px">
      <el-form-item label="手 机" prop="mobile">
        <el-input v-model="userInfoForm.mobile" />
      </el-form-item>
      <el-form-item label="入职时间">
        <el-date-picker v-model="userInfoForm.timeOfEntry" type="date" placeholder="选择日期" style="width: 100%;" />
      </el-form-item>
      <el-form-item label="员工头像">
        <upload-img v-model="userInfoForm.staffPhoto" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateInfo">保存更新</el-button>
        <router-link to="/employees" style="margin-left:15px"><el-button>返回</el-button> </router-link>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { saveUserDetailById } from '@/api/user'
export default {
  data() {
    return {
      userInfoForm: {
        userId: this.$route.params.id,
        mobile: '',
        timeOfEntry: '',
        staffPhoto: ''
      },
      rules: {
        mobile: [{ required: true, message: '手机号不能为空' }]
      }
    }
  },
  created() {
    this.loadUserDetail()
  },
  methods: {
    async loadUserDetail() {
      try {
        // const res = await getUserDetailById(this.userInfoForm.userId)
        this.getInfo(this.userInfoForm.userId)
        // this.userInfoForm = res.data
      } catch (err) {
        console.log('失败', err)
      }
    },
    updateInfo() {
      this.$refs.userInfoForm.validate(async valid => {
        if (!valid) return
        try {
          console.log(this.userInfoForm)
          await saveUserDetailById(this.userInfoForm)
          // 1. 提示
          this.$message.success('保存成功')
          // 2. 回到列表页
          this.$router.back()
        } catch (err) {
          console.log(err)
        }
      })
    }
  }
}
</script>
